const loginName=window.sessionStorage.getItem("loginName")
new Vue({
    el:'#i_bg',
    data:{
        imgStr:'http://192.168.2.106:8848/images/',
        infoFlag:false,
        infoText:'',
        collectionBox:[]
    },
    methods:{
        //获取收藏
        getCollectionBox()
        {
            axios.get("/api/getCollectionBox",
                {
                    params:{
                        loginName
                    }
                }).then(rs=>{
                this.collectionBox=rs.data.collectionBox
            })
        },
        //删除收藏
        delCollectById(id)
        {
            if(confirm("确认删除此条收藏吗"))
            {
                axios.get("/api/delCollectById",{
                    params:{
                        id,
                        loginName
                    }
                }).then(rs=>{
                    if(rs.data.isDel==="yes")
                    {
                        this.getCollectionBox()
                        this.infoText="删除成功"
                        this.infoFlag=true
                        setTimeout(()=>
                        {
                            this.infoFlag=false
                        },700)
                    }else{
                        this.getCollectionBox()
                        this.infoText="删除出错了"
                        this.infoFlag=true
                        setTimeout(()=>
                        {
                            this.infoFlag=false
                        },700)
                    }
                })
            }

        },
        addIntroCar(product)
        {
            if(confirm("是否加入购物车？"))
            {
                const formData=new FormData()
                let pro={id:product.id,name:product.name,price:product.price,fileName:product.fileName,num:1,
                    numPrice:product.price}
                formData.append("pro",JSON.stringify(pro))
                formData.append("loginName",loginName)
                axios.post('/api/car/addIntoCar',
                    formData
                ).then(rs=>{
                    if(rs.data.isAdd===true)
                    {
                        alert("加入购物车成功")
                        vm2.car=rs.data.car
                    }else if(rs.data.isAdd===false){
                        alert("库存不足")
                    }
                })
            }
        },
    },
    created()
    {
        this.getCollectionBox()
    }
})